package com.ruoyi.purchase.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.purchase.vo.MaterialReceiveVO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.purchase.domain.MaterialReceive;
import com.ruoyi.purchase.service.IMaterialReceiveService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 收料通知单Controller
 * 
 * @author ruoyi
 * @date 2025-10-29
 */
@RestController
@RequestMapping("/purchase/receive")
public class MaterialReceiveController extends BaseController
{
    @Autowired
    private IMaterialReceiveService materialReceiveService;

    /**
     * 扫码接口
     */
    @PreAuthorize("@ss.hasPermi('purchase:materialReceive:add')")
    @PostMapping("/scan")
    public AjaxResult scanCode(@RequestParam String code, @RequestParam boolean sameBatch) {
        System.out.println("扫码接口"+materialReceiveService.scanCode(code, sameBatch));
        return materialReceiveService.scanCode(code, sameBatch);
    }


    /**
     * 更新拒收状态
     */
    @PreAuthorize("@ss.hasPermi('purchase:materialReceive:edit')")
    @Log(title = "收料通知单", businessType = BusinessType.UPDATE)
    @PutMapping("/reject/{detailId}")
    public AjaxResult updateRejectStatus(@PathVariable Long detailId, @RequestParam Long isRejected) {
        return materialReceiveService.updateRejectStatus(detailId, isRejected);
    }




    /**
     * 查询收料通知单列表
     */
    @PreAuthorize("@ss.hasPermi('purchase:materialReceive:list')")
    @GetMapping("/list")
    public TableDataInfo list(MaterialReceive materialReceive) {
        startPage();
        List<MaterialReceiveVO> list = materialReceiveService.selectMaterialReceiveVOList(materialReceive);
        return getDataTable(list);
    }

    /**
     * 导出收料通知单列表
     */
    @PreAuthorize("@ss.hasPermi('purchase:receive:export')")
    @Log(title = "收料通知单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MaterialReceive materialReceive)
    {
        List<MaterialReceive> list = materialReceiveService.selectMaterialReceiveList(materialReceive);
        ExcelUtil<MaterialReceive> util = new ExcelUtil<MaterialReceive>(MaterialReceive.class);
        util.exportExcel(response, list, "收料通知单数据");
    }

    /**
     * 获取收料通知单详细信息
     */
    @PreAuthorize("@ss.hasPermi('purchase:materialReceive:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(materialReceiveService.selectMaterialReceiveVOById(id));
    }
    /**
     * 新增收料通知单
     */
    @PreAuthorize("@ss.hasPermi('purchase:receive:add')")
    @Log(title = "收料通知单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MaterialReceiveVO  materialReceiveVO)
    {
        return materialReceiveService.addMaterialReceiveWithDetails(materialReceiveVO);
    }

    /**
     * 修改收料通知单
     */
    @PreAuthorize("@ss.hasPermi('purchase:receive:edit')")
    @Log(title = "收料通知单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MaterialReceive materialReceive)
    {
        return toAjax(materialReceiveService.updateMaterialReceive(materialReceive));
    }

    /**
     * 删除收料通知单
     */
    @PreAuthorize("@ss.hasPermi('purchase:receive:remove')")
    @Log(title = "收料通知单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(materialReceiveService.deleteMaterialReceiveByIds(ids));
    }
}
